package cn.training.controller;

import java.sql.SQLException;

import java.util.List;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;

@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Model model) {
		UserBean userBean = new UserBean();
		model.addAttribute("userBean",userBean);//这句和上句是应用valid时加入的初始化，不使用时不需要加入
		return "userlogin";
	}

	// value = "/"相当于首页return "userlogin"，userlogin即为首页

	@RequestMapping(value = "/init", method = RequestMethod.POST)
	public String initLogin(UserBean userBean, Model model) {
		UserBean result = helloWorldService.searchUser(userBean);
		model.addAttribute("userBean", result);
		return "login";
	}

	// 通过searchUser从数据库查找，将结果返回到result

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String list(UserBean userBean, Model model) {
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("list", result);
		return "userlist";
	}

	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insert(UserBean userBean, Model model) {
		if (helloWorldService.insertUser(userBean) != 0)
			return "success";// 如果插入成功，返回success
		else
			return "helloWorld";
	}

	@RequestMapping(value = "/supdate", method = RequestMethod.POST)
	public String supdate(UserBean userBean, Model model) {
		UserBean result = helloWorldService.supdateUser(userBean);
		model.addAttribute("userBean", result);
		return "login";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update22(UserBean userBean, Model model) {
		if (helloWorldService.updateUser(userBean) != 0)// updateUser在UserSqlMap.xml中
			return "success";
		else
			return "helloWorld";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(UserBean userBean, Model model) {
		int delete = helloWorldService.deleteUser(userBean);
		model.addAttribute("list", delete);
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("list", result);
		return "userlist";
	}// 先删除，在查找出来
/**
	@RequestMapping(value = "/userLogin", method = RequestMethod.POST, params = "login")
	public String login(UserBean userBean, Model model) {
		String userId = userBean.getUserId();
		String password = userBean.getPassword();
		if (userId.isEmpty() || password.isEmpty()) {
			model.addAttribute("userlogin", "ID或密码不能为空！");
			return "userlogin";
		}
		UserBean res = helloWorldService.searchUser(userBean);

		if (res != null) {
			List<UserBean> result = helloWorldService.searchAll(userBean);
			model.addAttribute("userBean", result);
			return "index";
		} else {
			model.addAttribute("userlogin", "ID或密码错误！");
			return "userlogin";
		}

	}**/

	//下面是使用Valid时，验证登录的代码
	@RequestMapping(value = "/userLogin", method = RequestMethod.POST, params = "login")
	public String userLogin(Model model, HttpSession session,
			@Valid @ModelAttribute("userBean") UserBean userBean,
			BindingResult results) throws SQLException {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			return "update";
		}
		String userId = userBean.getUserId();
		String password = userBean.getPassword();
		if (userId.isEmpty() || password.isEmpty()) {
			model.addAttribute("userlogin", "ID或密码不能为空！");
			return "userlogin";
		}
		UserBean res = helloWorldService.searchUser(userBean);

		if (res != null) {
			List<UserBean> result = helloWorldService.searchAll(userBean);
			model.addAttribute("userBean", result);
			return "index";
		} else {
			model.addAttribute("userlogin", "ID或密码错误！");
			return "userlogin";
		}

	}
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public String index(UserBean userBean, Model model) {
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("userBean", result);
		return "index";
	}

	@RequestMapping(value = "/userLogin", method = RequestMethod.POST, params = "reg")
	public String reg(UserBean userBean, Model model) {
		return "register";
	}
    /**
	@RequestMapping(value = "/register", method = RequestMethod.POST, params = "userregister")
	public String register(HttpServletRequest req, UserBean userBean,
			Model model) {
		String password = req.getParameter("password");
		String repassword = req.getParameter("repassword");
		if (password.equals(repassword)) {
			if (helloWorldService.registerUser(userBean) != 0)
				return "userlogin";
			else
				return "register";
		} else {
			model.addAttribute("register", "密码和确认密码不一致！");
			return "register";
		}
	}**/
	@RequestMapping(value = "/register", method = RequestMethod.POST, params = "userregister")
	public String register(HttpServletRequest req,Model model, HttpSession session,
			@Valid @ModelAttribute("userBean") UserBean userBean,
			BindingResult results) throws SQLException  {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			return "update";
		}
		String password = req.getParameter("password");
		String repassword = req.getParameter("repassword");
		if (password.equals(repassword)) {
			if (helloWorldService.registerUser(userBean) != 0)
				return "userlogin";
			else
				return "register";
		} else {
			model.addAttribute("register", "密码和确认密码不一致！");
			return "register";
		}
	}
	@RequestMapping(value = "/userLogin", method = RequestMethod.POST, params = "upd")
	public String update1(UserBean userBean, Model model) {
		return "update";
	}

	 //@RequestMapping(value = "/update", method =
	// RequestMethod.POST,params="userupdate")
	// public String update(HttpServletRequest req,UserBean userBean,Model
	// model) {
	
	// String password = userBean.getPassword().toString();
	// String repassword= userBean.getRepassword().toString();
	// if(password.equals(repassword))
	// {
	// if(helloWorldService.updateUser(userBean)!=0)
	// {
	// model.addAttribute("update","修改成功");
	// return "userlogin";
	// }
	// else
	// return "update";
	// }
	// else
	// {
	// model.addAttribute("update","修改失败");
	// return "update";
	// }
	// }

	@RequestMapping(value = "/update", method = RequestMethod.POST,params="userupdate")
	public String update(Model model, HttpSession session,
			@Valid @ModelAttribute("userBean") UserBean userBean,
			BindingResult results) throws SQLException {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			return "update";
		}
		String password = userBean.getPassword().toString();
		String repassword= userBean.getRepassword().toString();
		if(password.equals(repassword))
		 {
		 if(helloWorldService.updateUser(userBean)!=0)
		 {
		 model.addAttribute("update","修改成功");
		 return "userlogin";
		 }
		 else
		 return "update";
		 }
		 else
		 {
		 model.addAttribute("update","修改失败");
		 return "update";
		 }
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST, params = "backlogin")
	public String backlogin(UserBean userBean, Model model) {
		return "userlogin";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST, params = "backlogin1")
	public String backlogin1(UserBean userBean, Model model) {
		return "userlogin";
	}

	@RequestMapping(value = "/deleteu", method = RequestMethod.POST, params = "deleten")
	public String back(UserBean userBean, Model model) {
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("userBean", result);
		return "index";
	}

	@RequestMapping(value = "/updateu", method = RequestMethod.POST, params = "updaten")
	public String back1(UserBean userBean, Model model) {
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("userBean", result);
		return "index";
	}

	@RequestMapping(value = "/delete1", method = RequestMethod.GET)
	public String delete1(UserBean userBean, Model model) {
		UserBean result = helloWorldService.okdeleteUser(userBean);
		model.addAttribute("userBean", result);
		return "confirm";
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update2(UserBean userBean, Model model) {
		UserBean result = helloWorldService.update2User(userBean);
		model.addAttribute("userBean", result);
		return "update";
	}

	@RequestMapping(value = "/deleteu", method = RequestMethod.POST, params = "deletey")
	public String deleteu(UserBean userBean, Model model) {
		int deleteu = helloWorldService.deleteUser(userBean);
		model.addAttribute("userBean", deleteu);
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("userBean", result);
		return "index";
	}

	@RequestMapping(value = "/updateu", method = RequestMethod.POST, params = "updatey")
	public String updateu(UserBean userBean, Model model) {
		int deleteu = helloWorldService.deleteUser(userBean);
		model.addAttribute("userBean", deleteu);
		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		model.addAttribute("userBean", result);
		return "index";
	}
}
